package com.john.bms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.john.bms.BmsConst;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class BaseController {

    protected String getCurrentUserName() {
        return getSession().getAttribute(BmsConst.USERNAME_KEY_IN_SESSION).toString();
    }

    protected void setUserInfoInSession(String userName) {
        HttpSession session = getSession();
        // 设置永不超时
        session.setMaxInactiveInterval(0);
        getSession().setAttribute(BmsConst.USERNAME_KEY_IN_SESSION, userName);
    }

    protected void removeUserInfoInSession() {
        getSession().removeAttribute(BmsConst.USERNAME_KEY_IN_SESSION);
    }

    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }


}
